namespace SinaFinance
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Browser;

    public static class CookieControl
    {
        public static void AddHistoary(string key)
        {
            string cookie;
            string[] history = GetHistory();
            if ((history != null) && (history.Length > 5))
            {
                cookie = "$" + history[2] + "$" + history[3] + "$" + history[4] + "$" + history[5];
            }
            else
            {
                cookie = GetCookie(SinaResources._SearchHistory);
            }
            SetCookie(SinaResources._SearchHistory, cookie + "$" + key);
        }

        public static void AddItemtoHistoryView(string item)
        {
            if (!Application.Current.IsRunningOutOfBrowser)
            {
                string cookie = GetCookie("cookie_history");
                List<string> list = null;
                if (!string.IsNullOrEmpty(cookie))
                {
                    list = cookie.Split(new char[] { '$' }).ToList<string>();
                    if (!list.Contains(item))
                    {
                        if (list.Count == 6)
                        {
                            SetCookie("cookie_history", cookie.Substring((int) (cookie.IndexOf('$') + 1)) + item + "$");
                        }
                        else
                        {
                            SetCookie("cookie_history", cookie + item + "$");
                        }
                    }
                    else
                    {
                        SetCookie("cookie_history", cookie.Replace(item + "$", ""));
                        cookie = GetCookie("cookie_history");
                        SetCookie("cookie_history", cookie + item + "$");
                    }
                }
                else
                {
                    SetCookie("cookie_history", cookie + item + "$");
                }
            }
        }

        public static string GetCookie(string key)
        {
            string[] strArray = HtmlPage.Document.Cookies.Split(new char[] { ';' });
            foreach (string str in strArray)
            {
                string[] strArray2 = str.Split(new char[] { '=' });
                if ((strArray2.Length == 2) && (strArray2[0].ToString() == key))
                {
                    return strArray2[1];
                }
            }
            return null;
        }

        public static string[] GetHistory()
        {
            if (GetCookie(SinaResources._SearchHistory) == null)
            {
                return null;
            }
            return GetCookie(SinaResources._SearchHistory).Split(new char[] { '$' });
        }

        private static List<string> LoadHistoryCookies()
        {
            if (!Application.Current.IsRunningOutOfBrowser)
            {
                string cookie = GetCookie("cookie_history");
                if (string.IsNullOrEmpty(cookie))
                {
                    return null;
                }
                return cookie.Split(new char[] { '$' }).ToList<string>();
            }
            return null;
        }

        public static void SetCookie(string key, string value)
        {
            DateTime time = DateTime.Now + TimeSpan.FromDays((double) 7.0);
            string str = key + "=" + value + ";expires=" + time.ToString("R");
            HtmlPage.Document.SetProperty("cookie", str);
        }
    }
}
